package controllers

import (
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"im/app/conf"
	"im/app/models"
	"im/app/models/form/friends"
	validator2 "im/app/models/validator"
	"im/app/services"
	"im/app/util/response"
	"strings"
)

type Friends struct {

}

// 好友列表
func(f *Friends) List(c *gin.Context) {
	inputs := friends.FriendsList{
		UserId:   0,
		Page:     1,
		PageSize: 10,
	}
	err := c.ShouldBind(&inputs)
	if err != nil {
		errs := err.(validator.ValidationErrors)
		response.ReturnJsonOk(c, validator2.OutErrors(errs))
		return
	}
	admin,_ := c.Get(models.ContextKeyUserObj)
	fs := services.FriendsService{}
	user := fs.GetUserFriendByUserId(admin.(*models.User).ID, inputs.Page, inputs.PageSize)
	// 判断是否在线
	for _,val := range user {
		if _,ok := ClientMap.Load(val.ID); ok {
			val.IsLive = true
		}
	}

	response.ReturnJsonOk(c, "成功", user)
}

// 添加好友
func(f *Friends) Add(c *gin.Context) {
	inputs := friends.FriendsAdd{}
	err := c.ShouldBind(&inputs)
	if err != nil {
		errs := err.(validator.ValidationErrors)
		response.ReturnJsonOk(c, validator2.OutErrors(errs))
		return
	}
	admin,_ := c.Get(models.ContextKeyUserObj)
	fs := services.FriendsService{}
	err = fs.AddFriend(admin.(*models.User).ID, inputs.UserId)
	if err != nil {
		response.ReturnJsonOk(c, err.Error())
		return
	}

	response.ReturnJsonOk(c, "成功", nil)
}

// 添加好友通过名称
func(f *Friends) AddByName(c *gin.Context) {
	inputs := friends.FriendsAddByName{}
	err := c.ShouldBind(&inputs)
	if err != nil {
		errs := err.(validator.ValidationErrors)
		response.ReturnJsonOk(c, validator2.OutErrors(errs))
		return
	}
	admin,_ := c.Get(models.ContextKeyUserObj)
	fs := services.FriendsService{}
	friend := models.User{}
	err = conf.Mdb.Where("name = ?", strings.Trim(inputs.UserName, " ")).Take(&friend).Error
	if err != nil {
		response.ReturnJsonOk(c, err.Error())
		return
	}
	err = fs.AddFriend(admin.(*models.User).ID, friend.ID)
	if err != nil {
		response.ReturnJsonOk(c, err.Error())
		return
	}

	response.ReturnJsonOk(c, "成功", nil)
}

// 删除好友
func(f *Friends) Delete(c *gin.Context) {
	inputs := friends.FriendsAdd{}
	err := c.ShouldBind(&inputs)
	if err != nil {
		errs := err.(validator.ValidationErrors)
		response.ReturnJsonOk(c, validator2.OutErrors(errs))
		return
	}
	admin,_ := c.Get(models.ContextKeyUserObj)
	fs := services.FriendsService{}
	err = fs.DeleteFriend(admin.(*models.User).ID, inputs.UserId)
	if err != nil {
		response.ReturnJsonOk(c, err.Error())
		return
	}

	response.ReturnJsonOk(c, "成功", nil)
}

// 好友信息
func(f *Friends) InfoFriend(c *gin.Context) {
	inputs := friends.FriendsAdd{}
	err := c.ShouldBind(&inputs)
	if err != nil {
		errs := err.(validator.ValidationErrors)
		response.ReturnJsonOk(c, validator2.OutErrors(errs))
		return
	}
	fs := services.FriendsService{}
	friend := fs.InfoFriend(inputs.UserId)

	response.ReturnJsonOk(c, "成功", friend)
}

// 搜索好友
func(f *Friends) Search(c *gin.Context) {
	inputs := friends.FriendsSearch{
		UserName: "",
		Page:     1,
		PageSize: 10,
	}
	err := c.ShouldBind(&inputs)
	if err != nil {
		errs := err.(validator.ValidationErrors)
		response.ReturnJsonOk(c, validator2.OutErrors(errs))
		return
	}
	fs := services.FriendsService{}
	friendList := fs.SearchFriend(inputs.UserName, inputs.Page, inputs.PageSize)

	response.ReturnJsonOk(c, "成功", friendList)  
}
